package threadpool.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: knowno
 * @create: 2024-09-14 14:06
 * @Version 1.0
 **/
public class TestFixedPool {
    public static void main(String[] args) {

        //1 创建一个线程池对象 【固定数量】
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        //模拟创建5个线程对象，通过池对象去管理
        for (int i = 1; i <= 10; i++) {
            Runnable r = () -> {
                System.out.println(Thread.currentThread().getName());
            };

            //使用线程不用去Thread->start方法...
            //2. 而是使用线程池对象的sumbmit方法，将Runnable对象作为参数传递进来即可.
            executorService.submit(r);
        }

        //3 关闭executorService
        executorService.shutdown();

    }
}
